package com.lbs.jiepiaisi.parser;

import android.util.Log;

import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.lbs.jiepiaisi.bean.TrackEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 时间：2017/9/15 11:26
 * 作用：实时跟踪解析类
 */

public class trackAccountParser  {
    //解析方法
    public static TrackEntity parser(String jsonString){

           TrackEntity trackEntity=new TrackEntity();

           List<TrackEntity.Result> results=new ArrayList<>() ;

           TrackEntity.Result result=new TrackEntity.Result();

        try {
            JSONObject jsonObject=new JSONObject(jsonString);
            trackEntity.setStatus(jsonObject.getInt("status"));
            JSONArray jsonArrayResult=jsonObject.getJSONArray("result");
            JSONObject   obj = jsonArrayResult.getJSONObject(0);
                //设置维度
                result.setGdLat(obj.getDouble("gdLat"));
                //设置经度
                result.setGdLng(obj.getDouble("gdLng"));
                //设置设备号
                result.setEqNo(obj.getString("eqNo"));
                //方向
                try {
                    result.setDir((float) obj.getDouble("dir"));
                }catch (Exception e){
                    Log.e("dir is nor","double居然用null 坑B");
                }
                try {
                    //速度
                    result.setSpeed(obj.getInt("speed"));
                }catch (Exception e){
                    Log.e("dir is nor","防止下坑B吧");
                }
                //时间
                result.setImportTime(obj.getString("importTime"));
                //时间
                result.setPositionTime(obj.getString("positionTime"));

                //状态
                result.setStatus(obj.getString("satatus"));
                //类型
                result.setPostType(obj.getString("postType"));

                results.add(0,result);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        trackEntity.setResults(results);
        return trackEntity;
    }
    //解析方法
    public static TrackEntity parser2(String jsonString){

        TrackEntity trackEntity=new TrackEntity();

        List<TrackEntity.Result> results=new ArrayList<>() ;

        TrackEntity.Result result=new TrackEntity.Result();

        try {
            JSONObject jsonObject=new JSONObject(jsonString);
            trackEntity.setStatus(jsonObject.getInt("status"));
            JSONArray jsonArrayResult=jsonObject.getJSONArray("result");
            JSONArray   obj = jsonArrayResult.getJSONObject(0).getJSONArray("items");
            //设置维度
            result.setGdLat(obj.getJSONObject(0).getJSONObject("point").getDouble("lat"));
            //设置经度
            result.setGdLng(obj.getJSONObject(0).getJSONObject("point").getDouble("lng"));
            //设置设备号
            result.setEqNo(obj.getJSONObject(0).getString("eqNo"));
            //方向
            try {
                result.setDir((float) obj.getJSONObject(0).getJSONObject("point").getDouble("dir"));
            }catch (Exception e){
                Log.e("dir is nor","double居然用null 坑B");
            }
            try {
                //速度
                result.setSpeed(obj.getJSONObject(0).getInt("speeding"));
            }catch (Exception e){
                Log.e("dir is nor","防止下坑B吧");
            }

            //时间
            result.setImportTime(obj.getJSONObject(0).getString("actTime"));
            //时间
            result.setPositionTime(obj.getJSONObject(0).getJSONObject("point").getString("positionTime"));

            //状态
            result.setStatus(obj.getJSONObject(0).getString("status"));
            //类型
            result.setPostType(obj.getJSONObject(0).getJSONObject("point").getString("postType"));

            results.add(0,result);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        trackEntity.setResults(results);
        return trackEntity;
    }

}
